package _go

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
var sum int

func convertBST(root *TreeNode) *TreeNode {
	if root == nil {
		return nil
	}
	sum = 0
	dfs(root)
	return root
}

func dfs(root *TreeNode) {
	if root == nil {
		return
	}
	dfs(root.Right)
	sum += root.Val
	root.Val = sum
	dfs(root.Left)
}
